package com.saya.model;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.saya.utils.Utils;

public class Order {
	 private String phonenumber;
	  private List<LineItemFull> lstProduct;
	  private String price;
	  private String oderdate;
	  private String status;
	  private String address;
	  private String customername;

	  public String getPhonenumber()
	  {
	    return this.phonenumber;
	  }
	  public void setPhonenumber(String phonenumber) {
	    this.phonenumber = phonenumber;
	  }
	  public List<LineItemFull> getLstProduct() {
	    return this.lstProduct;
	  }
	  public void setLstProduct(List<LineItemFull> lstProduct) {
	    this.lstProduct = lstProduct;
	  }
	  public String getPrice() {
	    return this.price;
	  }
	  public void setPrice(String price) {
	    this.price = price;
	  }
	  public String getOderdate() {
	    return this.oderdate;
	  }
	  public void setOderdate(String oderdate) {
	    this.oderdate = oderdate;
	  }
	  public String getStatus() {
	    return this.status;
	  }
	  public void setStatus(String status) {
	    this.status = status;
	  }
	  public String getAddress() {
	    return this.address;
	  }
	  public void setAddress(String address) {
	    this.address = address;
	  }
	  public String getCustomername() {
	    return this.customername;
	  }
	  public void setCustomername(String customername) {
	    this.customername = customername;
	  }

	  public static String Create(String phoneNumber, String customerName, String price, String address, List<LineItem> listItem)
	  {
	    String result = "0";
	    try {
	      Entity user = Utils.getEntity("User", phoneNumber);
	      Key userkey = user.getKey();
	      Entity order = new Entity("Order", userkey);
	      order.setProperty("PhoneNumber", phoneNumber);
	      if ((customerName == null) || (customerName.equals(""))) {
	        customerName = (String)user.getProperty("FullName");
	      }
	      order.setProperty("CustomerName", customerName);
	      if ((address == null) || (address.equals(""))) {
	        address = (String)user.getProperty("Address");
	      }
	      order.setProperty("Address", address);
	      order.setProperty("Price", price);
	      order.setProperty("Status", "Processing");
	      order.setProperty("OrderDate", Utils.getCurrentDate());
	      Key k = Utils.getDatastoreServiceInstance().put(order);

	      if (k == null) {
	        result = "0";
	      }
	      else {
	        for (LineItem lineItem : listItem)
	        {
	          Entity en = new Entity("LineItem", k);
	          en.setProperty("ItemId", lineItem.getId());
	          en.setProperty("Quantity", lineItem.getQuantity());
	          en.setProperty("Price", lineItem.getPrice());
	          Key k2 = Utils.getDatastoreServiceInstance().put(en);
	          if (k2 == null)
	            result = "0";
	          else
	            result = KeyFactory.keyToString(k2);
	        }
	      }
	    }
	    catch (Exception e)
	    {
	      result = "0";
	    }
	    return result;
	  }

	  public static List<Order> getAllOrdersForCustomer(String phonenumber, int offset, int limit) {
	    List lstOrder = new ArrayList();
	    Key customerKey = KeyFactory.createKey("User", phonenumber);
	    Iterable lstEntity = Utils.listChildren("Order", customerKey, offset, limit);
	    for (Object o : lstEntity) {
	      Entity entity = (Entity) o;
	      Order order = new Order();
	      if (entity.getProperty("CustomerName") != null) {
	        order.setCustomername((String)entity.getProperty("CustomerName"));
	      }
	      if (entity.getProperty("Price") != null) {
	        order.setPrice((String)entity.getProperty("Price"));
	      }
	      if (entity.getProperty("OrderDate") != null) {
	        order.setOderdate((String)entity.getProperty("OrderDate"));
	      }
	      if (entity.getProperty("Status") != null) {
	        order.setStatus((String)entity.getProperty("Status"));
	      }
	      if (entity.getProperty("Address") != null) {
	        order.setAddress((String)entity.getProperty("Address"));
	      }

	      List lstLineItem = new ArrayList();
	      Iterable<Entity> lineEntitys = Utils.listChildren("LineItem", entity.getKey(), 0, 0);
	      for (Entity e : lineEntitys) {
	        LineItemFull lineitem = new LineItemFull();
	        if (e.getProperty("ItemId") != null)
	          try {
	            String itemId = (String)e.getProperty("ItemId");
	            Entity itemEntity = Utils.getEntity(itemId);
	            Item item = new Item();
	            item.setId(itemId);
	            item.setName((String)itemEntity.getProperty("Name"));
	            item.setProduct((String)itemEntity.getProperty("Product"));
	            item.setPrice((String)itemEntity.getProperty("Price"));
	            item.setType((String)itemEntity.getProperty("Type"));
	            item.setIntroduction(
	              (String)itemEntity
	              .getProperty("Introduction"));
	            item.setUsage((String)itemEntity.getProperty("Usage"));
	            item.setNote((String)itemEntity.getProperty("Note"));
	            lineitem.setItem(item);
	          }
	          catch (Exception localException)
	          {
	          }
	        if (e.getProperty("Quantity") != null) {
	          lineitem.setQuantity((String)e.getProperty("Quantity"));
	        }
	        if (e.getProperty("Price") != null) {
	          lineitem.setPrice((String)e.getProperty("Price"));
	        }
	        lstLineItem.add(lineitem);
	        order.setLstProduct(lstLineItem);
	      }
	      lstOrder.add(order);
	    }

	    return lstOrder;
	  }
}
